<?php

namespace App\Admin\Controllers\User;

use App\Admin\Actions\Extensions\UniversityButton;
use App\Admin\Actions\Extensions\User\UserStudentCertifyButton;
use App\Admin\Actions\Grid\User\UserStudentCertifyNoticeAction;
use App\Admin\Repositories\User\WxUserStudentCertify;
use App\Wen\Utils\Settings;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class WxUserStudentCertifyController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new WxUserStudentCertify(), function (Grid $grid) {
            global $__MINI_GLOBAL_IS_ADMIN_SUPER__,$__MINI_GLOBAL_TENANT_ID__;
            $grid->quickSearch(['sid', 'school','user_id','user.user_name', 'email'])->placeholder('搜索sid，学校名称，用户ID，用户名字...')->width(50);
            $grid->model()->orderBy('updated_at','desc');
            if($__MINI_GLOBAL_TENANT_ID__ > 0){
                $grid->model()->whereIn('sid', Settings::get('tenant_'.$__MINI_GLOBAL_TENANT_ID__.'_with_student_certify', []));
            }
            $grid->column('users', '头像')->display(function ($v) {
                return '<img src="' . ($this->user['user_avatar'] ?? Settings::get('img_default', 'https://img.mini.minisns.cn/icons/dafault.png'))  . '"  style="border-radius:50px;width:30px;" />';
            })->width('50px');
            $grid->column('user')->display(function ($v) {
                if($v && $v['user_name']){
                    return '<a target="_blank" href="'.admin_url('users?id='.$v['id']).'">'.$v['user_name'].'</a>' ?? '用户已删除';
                }
                return '';
            })->width('100px');
            $grid->column('sid');
            $grid->column('school');
            $grid->column('email');
            $grid->column('role');
            $grid->column('term');
            $grid->column('questions');
            $grid->column('picture')->image();
            $grid->column('picture2')->image();
            $grid->column('introduce');
            $grid->column('status')->switch();
//            $grid->column('created_at')->sortable();
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('user_id');
            });

            $grid->disableCreateButton();
            $grid->disableBatchDelete();
            $grid->disableEditButton();
            $grid->disableDeleteButton();
            $grid->disableViewButton();

            if($__MINI_GLOBAL_TENANT_ID__ === 0){
                $grid->tools([
                    new UniversityButton(),
                    new UserStudentCertifyButton()
                ]);
            }else{
                $grid->tools([
                    new UniversityButton(),
                ]);
            }

            $grid->actions(function (Grid\Displayers\Actions $actions) {
                $actions->append((new UserStudentCertifyNoticeAction())->setKey($this->user_id));
            });

        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new WxUserStudentCertify(), function (Show $show) {
            $show->field('user_id');
            $show->field('sid');
            $show->field('school');
            $show->field('role');
            $show->field('term');
            $show->field('email');
            $show->field('questions');
            $show->field('picture');
            $show->field('picture2');
            $show->field('introduce');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
            $show->disableDeleteButton();
            $show->disableEditButton();
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new WxUserStudentCertify(), function (Form $form) {
            $form->select('user_id', '用户')->options(function ($id) {
                $user = \App\Models\User\WxUser::find($id);
                if ($user) {
                    return [$user->id => $user->user_name];
                }
            })->ajax('select/users');
            $form->text('sid');
            $form->text('school');
            $form->text('role');
            $form->text('term');
            $form->text('email');
            $form->text('questions');
            $form->text('picture');
            $form->text('picture2');
            $form->text('introduce');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');

            $form->saving(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    $user_id = $form->getKey();
                    $sid = \App\Models\User\WxUserStudentCertify::where('user_id', $user_id)->value('sid');
                    if(!in_array($sid, Settings::get('tenant_'.$__MINI_GLOBAL_TENANT_ID__.'_with_student_certify', []))){
                        return $form->response()->error('分站不能修改其他分站的学生认证');
                    }
                }
            });


            $form->deleting(function (Form $form){
                global $__MINI_GLOBAL_TENANT_ID__;
                if($__MINI_GLOBAL_TENANT_ID__ > 0){
                    $user_id = $form->getKey();
                    $sid = \App\Models\User\WxUserStudentCertify::where('user_id', $user_id)->value('sid');
                    if(!in_array($sid, Settings::get('tenant_'.$__MINI_GLOBAL_TENANT_ID__.'_with_student_certify', []))){
                        return $form->response()->error('权限不足，不可以删除其他分站对象');
                    }
                }
            });


        });
    }
}
